﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core;
using MvcEngine.Core.Helpers;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Utils;
using MvcEngine.Core.Base;
using MvcEngine.Gallery.Repositories;
using MvcEngine.Gallery.Models;

namespace MvcEngine.Gallery.Managers
{
    public class GalleryManager : BaseManager, IGalleryManager
    {
        private readonly IGalleryRepository galleryRepository = null;

        public GalleryManager(IGalleryRepository galleryRepository)
        {
            this.galleryRepository = galleryRepository;
        }

        public static IGalleryManager Current
        {
            get { return IoC.Resolve<IGalleryManager>(); }
        }

        public GalleryData GetGalleryByPage(Guid pageId)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.Page, pageId), GalleryCacheKeys.Category,
                () =>
                {
                    var gallery = galleryRepository.GetByPage(pageId);
                    ConvertXmlToProperties(gallery);
                    return gallery;
                });
        }

        public IList<GalleryData> GetGalleries()
        {
            return CacheManager.Current.FromCache(CacheUtils.MakeCacheKey(GalleryCacheKeys.All, CacheKeys.Site), GalleryCacheKeys.Category,
                () =>
                {
                    var galleries = galleryRepository.GetAll(SiteContext.Current.Site.Id);
                    foreach (var item in galleries)
                    {
                        ConvertXmlToProperties(item);
                    }
                    return galleries;
                });
        }

        public GalleryData GetGallery(Guid galleryId)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryId, galleryId), GalleryCacheKeys.Category,
                () =>
                {
                    var gallery = galleryRepository.Get(galleryId);
                    ConvertXmlToProperties(gallery);
                    return gallery;
                });
        }

        public GalleryData GetGallery(string galleryName)
        {
            return CacheManager.Current.FromCache(string.Format(GalleryCacheKeys.GalleryName, galleryName), GalleryCacheKeys.Category,
                () =>
                {
                    var gallery = galleryRepository.Get(galleryName);
                    ConvertXmlToProperties(gallery);
                    return gallery;
                });
        }

        public GalleryData GetCurrentGallery()
        {
            return GetGalleryByPage(SiteContext.Current.CurrentPage.Id);
        }

        public void Save(GalleryData gallery)
        {
            Guard.ThrowIfEmptyGuid(gallery.PageId, "PageId can't be empty.");
            using (var unitOfWork = UnitOfWorkHelper.Get())
            {
                var oldItem = galleryRepository.GetByPage(gallery.PageId);
                if (oldItem != null)
                {
                    oldItem.ModifiedDate = DateTime.Now;
                    oldItem.ModifiedBy = UserContext.Current.User.Id;
                    oldItem.Xml = gallery.Fields;
                    galleryRepository.Update(oldItem);
                }
                else
                {
                    gallery.ModifiedDate = DateTime.Now;
                    gallery.CreatedDate = DateTime.Now;
                    gallery.CreatedBy = UserContext.Current.User.Id;
                    gallery.ModifiedBy = UserContext.Current.User.Id;
                    gallery.Xml = gallery.Fields;
                    galleryRepository.Add(gallery);
                }
                unitOfWork.Commit();
            }
            CacheManager.Current.ClearCache(GalleryCacheKeys.Category);
        }
    }
}
